package com.ck.company.service.impl;

import com.ck.company.service.CpProgramLogService;
import com.ck.constant.BusinessConstant;
import com.ck.constant.RequestConstant;
import com.ck.dao.ProgramLogMapper;
import com.ck.model.common.ProgramLog;
import com.ck.model.common.User;
import com.ck.model.company.vo.CpProgramLogVO;
import com.ck.util.HttpUtils;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;

/**
 * 节目操作日志
 *
 * @author linqiang
 * @version v1.0
 * @date 2018-09-12 9:55
 */
@Service("cpProgramLogService")
public class CpProgramLogServiceImpl implements CpProgramLogService {

    @Autowired
    private ProgramLogMapper programLogMapper;

    @Override
    public int insert(ProgramLog record) {
        return programLogMapper.insert(record);
    }

    @Override
    public Page<CpProgramLogVO> listByPage(String programVersion, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        return programLogMapper.listByPage(programVersion);
    }

    @Override
    public void saveSuccessOperationLog(String programVersion, String optContent, HttpServletRequest request) {
        //保存操作日志
        User user = (User) request.getSession().getAttribute(RequestConstant.SESSION_KEY_USER);
        saveSuccessOperationLog(optContent, optContent, user);
    }

    @Override
    public void saveSuccessOperationLog(String programVersion, String optContent, User user) {
        //保存操作日志
        ProgramLog operationLog = new ProgramLog();
        operationLog.setOperationType(optContent);
        operationLog.setIsSuccess(BusinessConstant.OPERATION_LONG_OPERATION_STATUS_SUCCESS);
        operationLog.setAddTime(System.currentTimeMillis());
        if (null != user) {
            operationLog.setUserName(user.getUserName());
            operationLog.setRealname(user.getRealname());
        }
        operationLog.setProgramVersion(programVersion);
        programLogMapper.insert(operationLog);
    }

    @Override
    public void batchSaveSuccessOperationLog(String[] programVersionArr, String optContent, User user) {
        if (programVersionArr != null && programVersionArr.length > 0) {
            for (String aProgramVersionArr : programVersionArr) {
                saveSuccessOperationLog(aProgramVersionArr, optContent, user);
            }
        }
    }
}
